// Material Resource Node
// (c) jimon game studio

#ifndef JEH_IMATERIALRESOURCENODE
#define JEH_IMATERIALRESOURCENODE

#include "ISceneNode.h"
#include "jeMaterialResource.h"

namespace je
{
	namespace scene
	{
		//! Material Resource Node
		class IMaterialResourceNode:public je::scene::ISceneNode
		{
		protected:
			//! Current Shared Material
			media::jeSharedMaterial * CurrentSharedMaterial;

			//! Current Use Unique Material
			u1 CurrentUseUniqueMaterial;

			//! CurrentUseUniqueTexture
			u1 CurrentUseUniqueTexture;

			//! CurrentUseUniqueShader
			u1 CurrentUseUniqueShader;
		public:
			//! Constructor
			IMaterialResourceNode(video::IRender * Render,const jeStringd & SetNodeName)
				:ISceneNode(Render,SetNodeName),CurrentSharedMaterial(NULL),CurrentUseUniqueMaterial(false),CurrentUseUniqueTexture(false),CurrentUseUniqueShader(false)
			{
			}

			//! Destructor
			virtual ~IMaterialResourceNode()
			{
			}

			//! Set Material Resource
			virtual void SetMaterialResource(media::jeMaterialResource * MaterialResource,u1 UseUniqueMaterial = true,u1 UseUniqueTextures = true,u1 UseUniqueShader = true)
			{
				if(CurrentSharedMaterial)
				{
					if(CurrentUseUniqueMaterial)
						JE_MATERIALRESOURCE_DROP(CurrentSharedMaterial,this,CurrentRender)
					else
						JE_MATERIALRESOURCE_SHARED_DROP(CurrentSharedMaterial,CurrentRender)
					CurrentSharedMaterial = NULL;
				}

				CurrentUseUniqueMaterial = UseUniqueMaterial;
				CurrentUseUniqueTexture = UseUniqueTextures;
				CurrentUseUniqueShader = UseUniqueShader;

				if(MaterialResource)
				{
					if(CurrentUseUniqueMaterial)
						JE_MATERIALRESOURCE_GRAB(CurrentSharedMaterial,MaterialResource,this,CurrentUseUniqueTexture,CurrentUseUniqueShader,CurrentRender)
					else
						JE_MATERIALRESOURCE_SHARED_GRAB(CurrentSharedMaterial,MaterialResource,CurrentRender)
				}
			}

			//! Get Material Resource
			virtual media::jeMaterialResource * GetMaterialResource()
			{
				if(CurrentSharedMaterial)
					return JE_MATERIALRESOURCE(CurrentSharedMaterial->GetResourceName());
				else
					return NULL;
			}

			//! Get Shared Material Resource
			virtual media::jeSharedMaterial * GetSharedMaterialResource()
			{
				return CurrentSharedMaterial;
			}

			//! Serialize
			void Serialize(core::ISerializeStream * SerializeStream)
			{
				JE_SS_BEGIN(MaterialResourceNode)

				jeStringc MaterialResourceName;
				if(JE_SS_SAVE && CurrentSharedMaterial)
					MaterialResourceName = CurrentSharedMaterial->GetResourceName();

				JE_SS(MaterialResourceName)

				JE_SS_LINEAR_BLOCK_BEGIN(UniqueData)
				JE_SS(CurrentUseUniqueMaterial)
				JE_SS(CurrentUseUniqueTexture)
				JE_SS(CurrentUseUniqueShader)
				JE_SS_LINEAR_BLOCK_END(UniqueData)

				if(JE_SS_LOAD)
					SetMaterialResource(JE_MATERIALRESOURCE(MaterialResourceName),CurrentUseUniqueMaterial,CurrentUseUniqueTexture,CurrentUseUniqueShader);

				JE_SS_END(MaterialResourceNode)

				ISceneNode::Serialize(SerializeStream);
			}
		};
	}
}

#endif